clear*
set maxvar 11000

global date _2025_10_28
cd "/Users/atri0032/Dropbox/World Bank/Temperature, poverty, and inequality/Estimations/"

**************************************************************************************************************************************************************************
********************************** Table S13: The effects of temperature on poverty and inequality – Country level analysis using alternative data from WDI and SWIID
**************************************************************************************************************************************************************************

//==========================//	

*** Aggregate weather data at country level
* Temperature
use "${date}/_data/temperature.dta", clear

gen countrycode = substr(geo_code2_new,1,3)

collapse (mean) temp, by(countrycode year)

tempfile temperature
save `temperature'

* Rainfall
use "${date}/_data/rainfall.dta", clear

gen countrycode = substr(geo_code2_new,1,3)

collapse (mean) rainfall_sum, by(countrycode year)

tempfile rainfall
save `rainfall'

* Humidity
use "${date}/_data/humidity.dta", clear

gen countrycode = substr(geo_code2_new,1,3)

collapse (mean) humid, by(countrycode year)

tempfile humidity
save `humidity'

*** country/year in spid
use "${date}/_data/spid_for_analysis_v2.dta", clear

gen countrycode = code
qui reghdfe poor215 temp rainfall_sum humid , absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
keep if e(sample)

keep countrycode year
duplicates drop countrycode year, force

tempfile spid_list
save `spid_list'

*** Country chatacteristics
import delimited using "${date}/_data/WDI_additional.csv", clear

gen indicator = ""
	replace indicator = "GDPppp" if seriesname=="GDP per capita, PPP (constant 2021 international $)"
	replace indicator = "GDP" if seriesname=="GDP per capita (constant 2015 US$)"
	replace indicator = "Unemp" if seriesname=="Unemployment, total (% of total labor force) (national estimate)"
	replace indicator = "Unempilo" if seriesname=="Unemployment, total (% of total labor force) (modeled ILO estimate)"
	replace indicator = "popgr" if seriesname=="Population growth (annual %)"
	replace indicator = "agri" if seriesname=="Agriculture, forestry, and fishing, value added (% of GDP)"
	replace indicator = "manuf" if seriesname=="Manufacturing, value added (% of GDP)"
	replace indicator = "tech" if seriesname=="High-technology exports (% of manufactured exports)"
	replace indicator = "eduter" if seriesname=="Educational attainment, at least Bachelor's or equivalent, population 25+, total (%) (cumulative)"
	replace indicator = "edupri" if seriesname=="Educational attainment, at least completed primary, population 25+ years, total (%) (cumulative)"

drop seriesname seriescode

reshape long yr_, i(countrycode indicator) j(year)
reshape wide yr_, i(countrycode year) j(indicator) string
renvars yr_*, presub(yr_)
for var GDP GDPppp: gen ln_X = ln(X)

tempfile wdi
save `wdi'


*** WDI
use "${date}/_data/wdi.dta", clear

sort countrycode year
	merge 1:1 countrycode year using `temperature'
	drop if _merge==2
	drop _merge
	merge 1:1 countrycode year using `rainfall'
	drop if _merge==2
	drop _merge
	merge 1:1 countrycode year using `humidity'
	drop if _merge==2
	drop _merge	
	merge 1:1 countrycode year using `wdi'
	drop if _merge==2
	drop _merge	
	
	merge 1:1 countrycode year using `spid_list'
	keep if _merge==3
	drop _merge	
	
* FE model
	
local controls ln_GDPppp Unempilo agri manuf popgr 

estimates clear
qui reghdfe poor215 temp rainfall_sum humid `controls', absorb(countrycode year) cluster(countrycode) keepsingle	
	est store m1
qui reghdfe gini temp rainfall_sum humid `controls', absorb(countrycode year) cluster(countrycode) keepsingle	
	est store m2
	
local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2, fmt(0 3) ///
	label("Observations" "R-squared")) ///	
	eqlabels(none) mtitle("$2.15/day" "Gini WDI") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table S14 - Poverty data from WDI")
	
esttab m*, `opts'	
esttab m* using "${date}/_tables/_tab_s13.csv", replace `opts'
		

* Long difference model
preserve
bysort 	countrycode (year): egen year_min = min(year)
bysort 	countrycode (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort countrycode (year): gen n = _n
bysort countrycode (year): gen N = _N
	keep if N==2
	
encode countrycode, gen(id_country)	
xtset id_country n
foreach v in gini temp poor215 rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr  {
	gen `v'_d = D.`v'
}
	
local controls ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d   
	
estimates clear
qui reg poor215_d temp_d rainfall_sum_d humid_d `controls', cluster(id_country) 	
	est store m1
qui reg gini_d temp_d rainfall_sum_d humid_d `controls', cluster(id_country)  	
	est store m2
	
local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2, fmt(0 3) ///
	label("Observations" "R-squared")) ///	
	eqlabels(none) mtitle("$2.15/day" "Gini WDI") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table S14 - Poverty data from WDI")
	
esttab m*, `opts'	
esttab m* using "${date}/_tables/_tab_s13.csv", append `opts'	

restore


*** SWIID
use "${date}/_data/swiid.dta", clear

sort countrycode year
	merge 1:1 countrycode year using `temperature'
	drop if _merge==2
	drop _merge
	merge 1:1 countrycode year using `rainfall'
	drop if _merge==2
	drop _merge
	merge 1:1 countrycode year using `humidity'
	drop if _merge==2
	drop _merge	
	merge 1:1 countrycode year using `wdi'
	drop if _merge==2
	drop _merge		
	
	merge 1:1 countrycode year using `spid_list'
	keep if _merge==3
	drop _merge	
	
* FE model
local controls ln_GDPppp Unempilo agri manuf popgr 

estimates clear	
qui reghdfe gini_swiid temp rainfall_sum humid `controls', absorb(countrycode year) cluster(countrycode) keepsingle	
	est store m1
	
local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2, fmt(0 3) ///
	label("Observations" "R-squared")) ///	
	eqlabels(none) mtitle("Gini SWIID") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("WDI/SWIID analysis")	
	
esttab m*, `opts'	
esttab m* using "${date}/_tables/_tab_s13.csv", append `opts'	


* Long difference model

bysort 	countrycode (year): egen year_min = min(year)
bysort 	countrycode (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort countrycode (year): gen n = _n
bysort countrycode (year): gen N = _N
	keep if N==2
	
encode countrycode, gen(id_country)	
xtset id_country n
foreach v in gini_swiid temp rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr {
	gen `v'_d = D.`v'
}
	
local controls ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d   
	
estimates clear
qui reg gini_swiid_d temp_d rainfall_sum_d humid_d `controls', cluster(id_country) 	
	est store m2
	
local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2, fmt(0 3) ///
	label("Observations" "R-squared")) ///	
	eqlabels(none) mtitle("Gini SWIID") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table S14 - Inequality data from SWIID")
	
esttab m*, `opts'
esttab m* using "${date}/_tables/_tab_s13.csv", append `opts'

	